package com.yiduo.outpost.core.web.exception;

import com.yiduo.outpost.core.model.exception.ErrorConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.dao.DataIntegrityViolationException;

/**
 * @author yanwen
 */
@Slf4j
public class DuplicateKeyExceptionTranslator implements ExceptionTranslator {

    @Override
    public boolean support(Throwable error) {
        return error != null && DataIntegrityViolationException.class.isAssignableFrom(error.getClass());
    }

    @Override
    public String translateException(Throwable error, MessageSource messageSource) {
        return messageSource.getMessage(ErrorConstants.ERROR_ENTITY_REPEATED, null, LocaleContextHolder.getLocale());
    }
}
